package com.base.api.order;

import com.base.pay.connce.OrderQueryThreadFactoryImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class OrderAutoPoolService implements OrderAutoPoolExecutorService {
    private final ScheduledExecutorService automaticReceivingExecutors;/*自动确认收货*/

    private final ScheduledExecutorService cancelExecutors;/*未支付取消*/

    private final AutomaticReceivingRunnable automaticReceivingRunnable;

    private final CancelPayRunnable cancelPayRunnable;

    public OrderAutoPoolService(AutomaticReceivingRunnable automaticReceivingRunnable,
                                CancelPayRunnable cancelPayRunnable) {
        this.automaticReceivingRunnable = automaticReceivingRunnable;
        this.cancelPayRunnable = cancelPayRunnable;
        this.automaticReceivingExecutors = Executors.newSingleThreadScheduledExecutor(
                new OrderQueryThreadFactoryImpl("AutomaticReceivingExecutorsScheduledThread_")
        );
        this.cancelExecutors = Executors.newSingleThreadScheduledExecutor(
                new OrderQueryThreadFactoryImpl("CancelExecutorsScheduledThread_")
        );
    }

    @Override
    public void start() {
        /*确认收货*/
        this.automaticReceivingExecutors.scheduleAtFixedRate(automaticReceivingRunnable, 1, 3, TimeUnit.MINUTES);

        /*订单取消*/
        this.cancelExecutors.scheduleAtFixedRate(cancelPayRunnable,1,10,TimeUnit.SECONDS);
    }

    @Override
    public void shutdown() {
        automaticReceivingExecutors.shutdown();
    }

}
